package controllers

import (
	"findme-api/gpgga"
	"findme-api/models"
	"github.com/astaxie/beego"
	"strconv"
	"strings"
)

type QueryGpsController struct {
	beego.Controller
}

//func (c *QueryGpsController) GpsAllowCross() {
//	c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "http://localhost:14000")       //允许访问源
//	c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS")    //允许访问类型
//	c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "Content-Type,Authorization") //header的类型
//	c.Ctx.ResponseWriter.Header().Set("Access-Control-Max-Age", "1728000")
//	c.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Credentials", "true")
//	c.Ctx.ResponseWriter.Header().Set("content-type", "application/json") //返回数据格式是json
//}



//根据标签id查轨迹和历史足记
func (this *QueryGpsController) QueryOrbitById() {
    FileLogs.Info("===根据标签id查轨迹和历史足记===")
	message := this.GetString("subject")
	//fmt.Println(subject)
	//fmt.Println("=========message============:",message)
	//var allslice = []string{}
	orbitDataChan := make(chan []byte, 1000000)
	//gpshistoryfromDB(udpDataChan,message);
	models.ReadGps(orbitDataChan,message)
	//遍历信道，读取数据
	var i int = 0
	var m map[string]interface{}    //声明变量，不分配内存
	m = make(map[string]interface{}) //必不可少，分配内存

	var lenOrbtichan = len(orbitDataChan)
	//fmt.Println("orbitDatachan=====:",len(orbitDataChan))
	for item := range orbitDataChan{

		//fmt.Println("0000000000000000000000000get data from database0000000000000000000:",item)

		//过滤掉没有定位到数据的gps信息
		//log.Debug("111111111111111get data from database:",string(item))
		//log.Debug("311111111111111get data from database:",string(item)[67:68])
		//log.Debug("211111111111111get data from database:",string(item)[68:69])

		if string(item)[0:1] == "$" {
			if string(item)[28:29] == "N" && (string(item)[67:68] == "*" || string(item)[68:69] == "*") {

				//fmt.Println(len(string(item)))
				bb := gaggp.Gpgga(string(item))
				m[strconv.Itoa(i)] = bb
				if i == (lenOrbtichan - 1) {
					break
				}
				i++
				//fmt.Println("=====i=====:",i)
			}else {
				if i == (lenOrbtichan - 1) {
					break
				}
				i++
				continue
			}
		}else {
			capinfoarray := strings.Split(string(item), ",")
			//fmt.Println(capinfoarray[0])
			if capinfoarray[0] == "null" {
				if i == (lenOrbtichan - 1) {
					break
				}
				i++
                 continue
			}else {
				m[strconv.Itoa(i)] = capinfoarray[0] + "and" + capinfoarray[1]
				if i == (lenOrbtichan - 1) {
					break
				}
				i++
			}
		}
	}

	this.Data["json"] = m
	this.ServeJSON()
	this.StopRun()
}








